/**
 * Created by 89004804 on 2018/1/14.
 */
var listProviders = function (service) {
    var compiledTemplate = Template7.compile($('#service-provider-template').html());
    $.ajax({
        method : 'GET',
        url : '/api/service/'+ encodeURIComponent(service) +'/providers',
        dataType : 'json',
        success:function(result) {
            if(result) {
                var html = compiledTemplate({list:result});
                $("#service-provider-table tbody").html(html);
            }
        }
    });
};

var listConsumers = function (service) {
    var compiledTemplate = Template7.compile($('#service-consumer-template').html());
    $.ajax({
        method : 'GET',
        url : '/api/service/'+ encodeURIComponent(service) +'/consumers',
        dataType : 'json',
        success:function(result) {
            if(result) {
                var html = compiledTemplate({list:result});
                $("#service-consumer-table tbody").html(html);
            }
        }
    });
};
var bindEvent = function () {
    $('body').off('click.service-provider-enable')
        .on("click.service-provider-enable", ".service-provider-enable", function () {
            var _this = this;
            utils.message.confirm({content:'确定修改状态?', 
                yesBind : function () {
                    var address = $(_this).closest('tr').find('td.address').text();
                    var service = $('#modal-service-name').val();
                    var enable = $(_this).attr('enable') ==="true"? true : false;
                    $.ajax({
                        method : 'POST',
                        url : '/api/service/providers/status',
                        dataType : 'json',
                        contentType: "application/json",
                        data:JSON.stringify({service:service, address:address, enable:!enable}),
                        success:function(result) {
                            if(result && result.success) {
                                utils.showMessage(result.msg, "success");
                            }
                        }
                    });
                    return true;
                }
            });
        });
};


$(function () {
    var compiledTemplate = Template7.compile($('#service-template').html());
    $.ajax({
        method : 'GET',
        url : '/api/service/list',
        dataType : 'json',
        success:function(result) {
            if(result) {
                var html = compiledTemplate({list:result});
                $("#service-table tbody").html(html);
            }
        }
    });

    $('body').off('click.service-list-edit')
        .on("click.service-list-edit", ".service-list-edit", function () {
            var service = $(this).parent('td').prev().text();
            $('#modal-service-name').val(service);
            $('#detail-dialog').on('shown.bs.modal', function () {
                $('#service-modal-title').text('服务名:'+service);
                listProviders(service);
                listConsumers(service);
                bindEvent();
            }).modal('show');
    });
});